use strict;
use warnings;
#use Test::Output;

use Test::More;
use ReadSam;

create_test_files();

my $sam = ReadSam->new("test.sam");

my $header = $sam->header;

use Data::Dumper;

is(scalar keys %{ $header->tags }, 3, "parse sam header");
is($header->lines =~ tr/\n/\n/, 4, "sam header lines");

my $align = $sam->next_align;

like($align->qname, qr{HWI-ST909:137:C17C3ACXX:}, "qname");

is($align->string =~ tr/\t/\t/, $align->raw =~ tr/\t/\t/, "sam align parsing");

my $array = $align->quality_array();
$array->[0] = 10;
$align->quality_array($array);

is(substr($align->quality,0,1),chr(32+10), "modify quality");


$align->quality_at(1,11);
is(substr($align->quality,1,1),chr(32+11), "quality_at");

is($align->end, 34,"end");

$sam->close;

is($sam->next_align, undef, "read on closed");

my $sam2 = ReadSam->new("test.bam");

my $header2 = $sam2->header;

is(scalar keys %{ $header2->tags }, 3, "bam parse header");

is($header2->lines =~ tr/\n/\n/, 4, "bam header lines");

my $align_bam = $sam2->next_align;

like($align_bam->qname, qr{HWI-ST909:137:C17C3ACXX:}, "bam qname");

is($align_bam->string =~ tr/\t/\t/, $align_bam->raw =~ tr/\t/\t/, "bam align parsing");

unlink qw(test.sam test.bam);

done_testing();

sub create_test_files{
  open my $sam, '>', "test.sam";
  print $sam 
'@HD	VN:1.0	SO:coordinate
@SQ	SN:adapter1	LN:33
@SQ	SN:adapter2	LN:33
@PG	ID:bowtie2	PN:bowtie2	CL:/usr/local/bin/bowtie2 --local --very-sensitive-local -p 8 -x primers -U /SerreDLab/raw_reads/2012-09-24_MTL/7_DS69.E18-5_44L_R1.fastq.gz --no-unal	VN:2.0.0-beta7
HWI-ST909:137:C17C3ACXX:7:1101:1851:1950	0	adapter1	1	22	115S33M2S	*	0	0	NGGGGTTTGGGTATATTTTTTTAAAAAGATACGTTTTAGTTAGGCGTGGTGGCGTACGTTTCTAATTTTAGTATTCGAGAGGTAGAGGTAGGTGGATTTTTGTTTGGGGTTTTTTAGATCGGAAGAGCACACGTCTGAACTCCAGTCACC	#1:DDDDDHHHDFHIIJJJJJJIIJJJGIGIHGBGIIIIIJGHHHIAFH@EHHF>DDD@CDDD>DDEEDEDCDEEBCDCDDD:CCCDD@ACDACD?CCCDDDDDDC@DD9<@DDD@C@CABDDDDDDDDDC?52898CCC##########	AS:i:66	XN:i:0	XM:i:0	XO:i:0	XG:i:0	NM:i:0	MD:Z:33	YT:Z:UU	XR:Z:@HWI-ST909%3A137%3AC17C3ACXX%3A7%3A1101%3A1851%3A1950%201%3AN%3A0%3ACGAAGG%0ANGGGGTTTGGGTATATTTTTTTAAAAAGATACGTTTTAGTTAGGCGTGGTGGCGTACGTTTCTAATTTTAGTATTCGAGAGGTAGAGGTAGGTGGATTTTTGTTTGGGGTTTTTTAGATCGGAAGAGCACACGTCTGAACTCCAGTCACC%0A+%0A#1%3ADDDDDHHHDFHIIJJJJJJIIJJJGIGIHGBGIIIIIJGHHHIAFH@EHHF>DDD@CDDD>DDEEDEDCDEEBCDCDDD%3ACCCDD@ACDACD?CCCDDDDDDC@DD9<@DDD@C@CABDDDDDDDDDC?52898CCC##########%0A
HWI-ST909:137:C17C3ACXX:7:1101:10745:1937	0	adapter1	1	22	54S33M63S	*	0	0	NGTCGTTGTAAAGGTTAGAGAGGATAGGTGGGATATAGGTGGGATATTTTTTTAAGATCGGAAGAGCACACGTCTGAACTCCAGTCACCGAAGGATCTCGTATGCCGTCTTCTGCTTGAAAAAAAAAAAAAAAAACAAAACACTCAACAG	#1=ADDDFHHHHHJFHIJJJJJJJJJJJ9EHIGGJJJJJ?GHIJIJFIJJIJJIJFHHHHFFFDEEEEDDDDDDDDDDDDDDCDDDDCDDDDDDB43@ABBBDDDDD5>BBCC@CDDCDDD>AD##########################	AS:i:66	XN:i:0	XM:i:0	XO:i:0	XG:i:0	NM:i:0	MD:Z:33	YT:Z:UU	XR:Z:@HWI-ST909%3A137%3AC17C3ACXX%3A7%3A1101%3A10745%3A1937%201%3AN%3A0%3ACGAAGG%0ANGTCGTTGTAAAGGTTAGAGAGGATAGGTGGGATATAGGTGGGATATTTTTTTAAGATCGGAAGAGCACACGTCTGAACTCCAGTCACCGAAGGATCTCGTATGCCGTCTTCTGCTTGAAAAAAAAAAAAAAAAACAAAACACTCAACAG%0A+%0A#1=ADDDFHHHHHJFHIJJJJJJJJJJJ9EHIGGJJJJJ?GHIJIJFIJJIJJIJFHHHHFFFDEEEEDDDDDDDDDDDDDDCDDDDCDDDDDDB43@ABBBDDDDD5>BBCC@CDDCDDD>AD##########################%0A
HWI-ST909:137:C17C3ACXX:7:1101:14425:1955	0	adapter1	1	22	110S33M7S	*	0	0	NGGGGTTTGGGTATATTTTTTTAAAGTTAATTTTTAAGATGAGATTGGTTTTATTAGATTTGAGAGGATGTTTGAGTTATTTAAATTTAGATTTTTGTTTGGGGTTTTTTAGATCGGAAGAGCACACGTCTGAACTCCAGTCACCGAAGG	#1=DDDDDFHHFHIJJJJJJJJIJJJIHIIJJJJJJJJIJJIJJJJJJBGIIJJJJIIIJJJEHEFGFFF@DEEECECCEDEFDCDEDDDDDEDDDDDDDD@BD<<BDDDDDDDDBDDDBDBDCBDDBBDDDDDDDDCDCCDCCDDBBB#	AS:i:66	XN:i:0	XM:i:0	XO:i:0	XG:i:0	NM:i:0	MD:Z:33	YT:Z:UU	XR:Z:@HWI-ST909%3A137%3AC17C3ACXX%3A7%3A1101%3A14425%3A1955%201%3AN%3A0%3ACGAAGG%0ANGGGGTTTGGGTATATTTTTTTAAAGTTAATTTTTAAGATGAGATTGGTTTTATTAGATTTGAGAGGATGTTTGAGTTATTTAAATTTAGATTTTTGTTTGGGGTTTTTTAGATCGGAAGAGCACACGTCTGAACTCCAGTCACCGAAGG%0A+%0A#1=DDDDDFHHFHIJJJJJJJJIJJJIHIIJJJJJJJJIJJIJJJJJJBGIIJJJJIIIJJJEHEFGFFF@DEEECECCEDEFDCDEDDDDDEDDDDDDDD@BD<<BDDDDDDDDBDDDBDBDCBDDBBDDDDDDDDCDCCDCCDDBBB#%0A
HWI-ST909:137:C17C3ACXX:7:1101:15263:1988	0	adapter1	1	22	56S33M61S	*	0	0	NTAGTGGGTTTAGGGTGAGTGTAGTGAAAGGTAAATTAAATATTTTTGGGTAGGGGAGATCGGAAGAGCACACGTCTGAACTCCAGTCACCGAAGGATCTCGTATGCCGTCTTCTGCTTGAAAAAAAAAATAAGCAGAATCTAATCAAAA	#1=BDDEFFHHGHJJEFHGHHHHGEHIJIIJGHHIIJIIJJJJJIJJJJI?BFHI>HIGJJ=EHFFFEDACEDDA?CBCDDDDDDDDDDC@@BDDDB@ACCB?BDDDC@>B@BAA:@ACAAADD##########################	AS:i:66	XN:i:0	XM:i:0	XO:i:0	XG:i:0	NM:i:0	MD:Z:33	YT:Z:UU	XR:Z:@HWI-ST909%3A137%3AC17C3ACXX%3A7%3A1101%3A15263%3A1988%201%3AN%3A0%3ACGAAGG%0ANTAGTGGGTTTAGGGTGAGTGTAGTGAAAGGTAAATTAAATATTTTTGGGTAGGGGAGATCGGAAGAGCACACGTCTGAACTCCAGTCACCGAAGGATCTCGTATGCCGTCTTCTGCTTGAAAAAAAAAATAAGCAGAATCTAATCAAAA%0A+%0A#1=BDDEFFHHGHJJEFHGHHHHGEHIJIIJGHHIIJIIJJJJJIJJJJI?BFHI>HIGJJ=EHFFFEDACEDDA?CBCDDDDDDDDDDC@@BDDDB@ACCB?BDDDC@>B@BAA%3A@ACAAADD##########################%0A';
  close $sam;

  open my $bam, "| samtools view - -S -b -o test.bam";
print $bam 
'@HD	VN:1.0	SO:coordinate
@SQ	SN:adapter1	LN:33
@SQ	SN:adapter2	LN:33
@PG	ID:bowtie2	PN:bowtie2	CL:/usr/local/bin/bowtie2 --local --very-sensitive-local -p 8 -x primers -U /SerreDLab/raw_reads/2012-09-24_MTL/7_DS69.E18-5_44L_R1.fastq.gz --no-unal	VN:2.0.0-beta7
HWI-ST909:137:C17C3ACXX:7:1101:2558:2243	0	adapter1	1	22	56S33M61S	*	0	0	GTAAGTTGGTTGGTATTTATTTTAGGGAAAGGTGGAGAAGGTGGTTTTTGTTAATAAGATCGGAAGAGCACACGTCTGAACTCCAGTCACCGAAGGATCTCGTATGCCGTCTTCTGCTTGAAAAAAACAACACAAATAACAACAAGAGCA	+:?DDDDFHFHHGAFHJJJJJJJJIJIIIGHIGHIGIGHGJBGGGIBFHIGGGIIJGIHIJDIFEHGFFFEEEC>ACDDDDDDDDDDDAAC@5@BDD@CCDDD7>ACCD@DDD@C>C@CC?C>@A#########################	AS:i:66	XN:i:0	XM:i:0	XO:i:0	XG:i:0	NM:i:0	MD:Z:33	YT:Z:UU	XR:Z:@HWI-ST909%3A137%3AC17C3ACXX%3A7%3A1101%3A2558%3A2243%201%3AN%3A0%3ACGAAGG%0AGTAAGTTGGTTGGTATTTATTTTAGGGAAAGGTGGAGAAGGTGGTTTTTGTTAATAAGATCGGAAGAGCACACGTCTGAACTCCAGTCACCGAAGGATCTCGTATGCCGTCTTCTGCTTGAAAAAAACAACACAAATAACAACAAGAGCA%0A+%0A+%3A?DDDDFHFHHGAFHJJJJJJJJIJIIIGHIGHIGIGHGJBGGGIBFHIGGGIIJGIHIJDIFEHGFFFEEEC>ACDDDDDDDDDDDAAC@5@BDD@CCDDD7>ACCD@DDD@C>C@CC?C>@A#########################%0A
HWI-ST909:137:C17C3ACXX:7:1101:9592:2133	0	adapter1	1	22	76S33M41S	*	0	0	AGGGGTTTGGGTATATTTTTTTAAATTGTTATGTGGATGTTTTTTTTTTTTTTATGATTTTTGTTTGGGGTTTTTTAGATCGGAAGAGCACACGTCTGACCTCCAGTCACCGAAGGATCTCGTATGCCGTCTTCTGCTTGAAAAAACAAA	+?@DFFDFFHFFDIJJJJJJJJJIHIGFHHGIGIIIJIIIJJJJJJHFDDDDB(::(:BCDC7ACCABDBBDBBDB?<>C>?<@##################################################################	AS:i:62	XN:i:0	XM:i:1	XO:i:0	XG:i:0	NM:i:1	MD:Z:23A9	YT:Z:UU	XR:Z:@HWI-ST909%3A137%3AC17C3ACXX%3A7%3A1101%3A9592%3A2133%201%3AN%3A0%3ACGAAGG%0AAGGGGTTTGGGTATATTTTTTTAAATTGTTATGTGGATGTTTTTTTTTTTTTTATGATTTTTGTTTGGGGTTTTTTAGATCGGAAGAGCACACGTCTGACCTCCAGTCACCGAAGGATCTCGTATGCCGTCTTCTGCTTGAAAAAACAAA%0A+%0A+?@DFFDFFHFFDIJJJJJJJJJIHIGFHHGIGIIIJIIIJJJJJJHFDDDDB(%3A%3A(%3ABCDC7ACCABDBBDBBDB?<>C>?<@##################################################################%0A
HWI-ST909:137:C17C3ACXX:7:1101:10180:2065	0	adapter1	1	22	62S33M55S	*	0	0	TTAGTGGGTTTAGGGTGAGTGTAGTGAAAGGGAAAGGTAAATTAAATATTTTTGGGTAGGGGAGATCGGAAGAGCACACGTCTGAACTCCAGTCACCGAAGGATCTCGTATGCCGTCTTCTGCTTGAAAAAAAAAAAACAATACTAACAA	:BBFFFFFHHHHHJJ2AFHCHFHHCGIIJJJJJJIJJDGIJJJJIJGJIGJJJIJJCFHIJIIHHFFFDDDDDDDABDDDDDCDDDDDDDDDDDDDDDDDDD@A9@C?8<ACCDD5<?8ACAACDABDCCA###################	AS:i:66	XN:i:0	XM:i:0	XO:i:0	XG:i:0	NM:i:0	MD:Z:33	YT:Z:UU	XR:Z:@HWI-ST909%3A137%3AC17C3ACXX%3A7%3A1101%3A10180%3A2065%201%3AN%3A0%3ACGAAGG%0ATTAGTGGGTTTAGGGTGAGTGTAGTGAAAGGGAAAGGTAAATTAAATATTTTTGGGTAGGGGAGATCGGAAGAGCACACGTCTGAACTCCAGTCACCGAAGGATCTCGTATGCCGTCTTCTGCTTGAAAAAAAAAAAACAATACTAACAA%0A+%0A%3ABBFFFFFHHHHHJJ2AFHCHFHHCGIIJJJJJJIJJDGIJJJJIJGJIGJJJIJJCFHIJIIHHFFFDDDDDDDABDDDDDCDDDDDDDDDDDDDDDDDDD@A9@C?8<ACCDD5<?8ACAACDABDCCA###################%0A
HWI-ST909:137:C17C3ACXX:7:1101:10080:2181	0	adapter1	1	22	80S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